﻿using M_Process.API.Appliction.Command.ProcessTasksCommand;
using M_Process.Domain;
using M_Process.Domain.DTO;
using M_Process.Repository.ProcessImpl;
using M_Process.Repository.ProcessInterface;
using MediatR;

namespace M_Process.API.Appliction.CommandHandlers.ProcessTasksCommandHandler
{
    /// <summary>
    /// 显示工序任务命令处理器
    /// </summary>
    public class ShowProcessTasksCommandHandler : IRequestHandler<ShowProcessTasksCommand, ProcessTasksDto<ProcessTasks>>
    {
        /// <summary>
        /// 工序任务仓储接口
        /// </summary>
        protected readonly IProcessTasksRepository processTasksRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="processTasksRepository">工序任务仓储接口</param>
        public ShowProcessTasksCommandHandler(IProcessTasksRepository processTasksRepository)
        {
            this.processTasksRepository = processTasksRepository;
        }

        /// <summary>
        /// 处理显示工序任务命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ProcessTasksDto<ProcessTasks>> Handle(ShowProcessTasksCommand request, CancellationToken cancellationToken)
        {
            var list = from a in await processTasksRepository.GetAllAsync()
                       where !a.IsDeleted
                       select new ProcessTasks()
                       {
                           ProcessTasksId = a.ProcessTasksId,
                           TaskNumber = a.TaskNumber,
                           TaskStatus = a.TaskStatus,
                           Priority = a.Priority,
                           ProductionBatch = a.ProductionBatch,
                           ProcessName = a.ProcessName,
                           ProductNumber = a.ProductNumber,
                           CreatedBy = a.CreatedBy,
                           CreatedDate = a.CreatedDate,
                           UpdatedBy = a.UpdatedBy,
                           UpdatedDate = a.UpdatedDate,
                           IsDeleted = a.IsDeleted
                       };
            if (!string.IsNullOrEmpty(request.TaskNumber))
            {
                list = list.Where(x => x.TaskNumber.Contains(request.TaskNumber));
            }

            if (!string.IsNullOrEmpty(request.TaskStatus))
            {
                list = list.Where(x => x.TaskStatus.Contains(request.TaskStatus));
            }

            if (request.PageIndex == null)
            {
                request.PageIndex = 1;
            }

            if (request.PageSize == null)
            {
                request.PageSize = 15;
            }

            ProcessTasksDto<ProcessTasks> page = new ProcessTasksDto<ProcessTasks>();
            page.TotalCount = list.Count();
            page.PageCount = (int)Math.Ceiling(page.TotalCount * 1.0 / (int)request.PageSize);
            page.list = list.OrderByDescending(x => x.CreatedDate).Skip((int)request.PageIndex - 1).Take((int)request.PageSize).ToList();
            return page;
        }
    }
}
